package com.skyger.tool;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;

public class FileTool {
	/**
	 * 
	 * @param filename
	 * @return
	 */
	public static StringBuffer readFileContent(String filename){
		StringBuffer buffer = new StringBuffer();
		String line = null;
        BufferedReader reader=null;
        try {  
        	InputStream is = new FileInputStream(filename);  
        	reader = new BufferedReader(new InputStreamReader(is,  
                "UTF-8"));  
            line = reader.readLine();  
        } catch (IOException e) {
            e.printStackTrace();  
        }
        while (line != null) {
            buffer.append(line);
            buffer.append("\r\n");
            try {  
                line = reader.readLine();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }
        }
		return buffer;
	}
	
	/**
	 * 把文本写入文件中
	 * @param filename
	 * @param fileContent
	 * @return
	 */
	public static void writeFileContent(String filename,String fileContent){
		try {  
            File f = new File(filename);  
            if (!f.getParentFile().exists()) {  
                f.getParentFile().mkdirs();  
            }  
            FileOutputStream out = new FileOutputStream(filename);  
            byte[] bytes = fileContent.getBytes("UTF-8");  
            out.write(bytes);  
            out.flush();  
            out.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
	}
	
	public static int getFileLineNum(String filename){
		int num =0;
		String line = null;
        BufferedReader reader=null;
        try {  
        	InputStream is = new FileInputStream(filename);  
        	reader = new BufferedReader(new InputStreamReader(is));  
            line = reader.readLine();  
        } catch (IOException e) {
        	System.out.println(line);
            e.printStackTrace();  
        }
        while (line != null) {
            try {  
            	num++;
                line = reader.readLine();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }
        }
        try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return num;
	}
	
}
